package 路径问题;

import java.util.Scanner;

public class test5 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] nums  = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                nums[i][j] = in.nextInt();
            }
        }
        System.out.println(minPathSum(nums));
    }
    public static int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m+2][n+2];

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if(j==1)
                    dp[i][j] += (dp[i-1][j]+grid[i-1][j-1]); //只能从上面下来
                else if (i==1) {
                    dp[i][j] += (dp[i][j-1]+grid[i-1][j-1]); // 只能从左边过来
                } else
                    dp[i][j] = Math.min(dp[i][j-1],dp[i-1][j])+grid[i-1][j-1];
            }
        }
        return dp[m][n];
    }
}
